<template>
  <div class="tree-item">
    <span class="i1">{{ item.name }}</span>
    <span class="i2">{{ item.manager }}</span>
    <!-- 
          el-dropdown @command="function(command){}"
              slot:dropdown 下拉 列表
                   default:显示的文本内容
            下拉列表
           el-dropdown-menu
               el-dropdown-item  command="标识"



         -->
    <el-dropdown class="i3" @command="commandEvent">
      <span>
        <span>操作</span>
        <i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item v-if="!item.isTop" command="look"
            >查看部门</el-dropdown-item
          >
          <el-dropdown-item v-if="!item.isTop" command="del"
            >删除部门</el-dropdown-item
          >
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script>
import { companyDepartmentDel } from '@/api/departments.js'
export default {
  name: 'TreeItem',
  props: ['item'], // 当前点击项的数据
  methods: {
    commandEvent (command) {
      switch (command) {
        case 'add':
          // 打开弹框，传递当前点击项数据
          this.$bus.$emit('show', true, this.item)
          break
        case 'del':
          // this.$confirm(内容,标题，{cancelButtonText:取消按钮文本,confirmButtonText:确定按钮文本,type:}).then(成功).catch(取消回调)
          this.$confirm('您确定删除该部门吗？', '提示')
            .then(async () => {
              await companyDepartmentDel(this.item.id)
              this.$message.success('删除部门成功')
              this.$emit('getData')
            })
            .catch(() => {})
          break
        case 'look':
          console.log('点击项数据:', this.item)
          this.$bus.$emit('show', true, this.item, 'edit')
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.tree-item {
  display: flex;
  align-items: center;
  .i1 {
    flex: 1;
    font-weight: bold;
  }
  .i2 {
    width: 150px;
    text-align: right;
  }
  .i3 {
    width: 150px;
    text-align: right;
  }
}
</style>
